package com.ckf.login_wx;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ckf.login_wx.util.HttpContextUtils;

@Component
public class MyInterceptor implements HandlerInterceptor{
	@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
		HttpServletRequest requestf = HttpContextUtils.getHttpServletRequest();
        String ipAddress = HttpContextUtils.getIpAddress();
        System.out.println("ip:"+ipAddress);
        
        
        
        
		String url =request.getRequestURI();
		//拦截
		ipAddress="ip:"+ipAddress;
		if(ipAddress.indexOf("ip:34.")!=-1||ipAddress.equals("ip:42.236.10.106")){
            System.out.println("=====已拦截ip:"+ipAddress);
			return false;
		}
    	if(url.indexOf("/Sp/")==-1&&url.indexOf("error")==-1){
            System.out.println("=====小程序接口 ===="+url);
    		return true;//保证小程序的接口请求不被拦截
    	}
        System.out.println("=====进入拦截 ===="+url);
        HttpSession session= request.getSession();
        String username = session.getAttribute("username")+"";
        String password = session.getAttribute("password")+"";
        String quxian = session.getAttribute("quxian")+"";
        //System.out.println(username +","+password);

//    	if(username.equals("null")||password.equals("null")){
//    		//response.setStatus(403);
//    		response.sendError(403, "您没有权限");
//    		//response.sendRedirect("/login.html");
//    		return false;
//    	}
        //quxian:4，超管不拦截return true
        if(quxian.equals("4")){
        	return true;
        }
        //quxian:4，企业管理员
        if(quxian.equals("3")){
        	String [] k3={};
        	List<String> List3 = Arrays.asList(k3);
        	if(List3.contains(url)){      
        		return false;
        	}
        	return true;
        }
        //quxian:2，销售
        if(quxian.equals("2")){
        	String [] k2={"/Sp/userlistall","/Sp/seleusersearch",
        			"/Sp/seleuserbyid","/Sp/adduser","/Sp/selematerialbyid","/Sp/addmate","/Sp/selematesearch","/Sp/sele",
        			"/Sp/selebillbyid","/Sp/incomelistall","/Sp/selebillbysearch","/Sp/addordex","/Sp/seledisbursebyid",
        			"/Sp/exlistall","/Sp/exlistsearch"};//,"/Sp/seleoddbytotal","/Sp/billinfo","/Sp/oddinfo"   
        	List<String> List2 = Arrays.asList(k2);
        	if(url.indexOf("dele")!=-1){ //任何删除都会被拦截
        		return false;
        	}
        	if(List2.contains(url)){    
        		return false;
        	}
        	return true;
        }
        //quxian:1，制单员，
        if(quxian.equals("1")){
        	String [] k1={"/Sp/userlistall","/Sp/seleusersearch","/Sp/seleuserbyid","/Sp/adduser"};
        	List<String> List1 = Arrays.asList(k1);
        	if(url.indexOf("dele")!=-1){//任何删除都会被拦截     
        		return false;
        	}
        	if(List1.contains(url)){      
        		return false;
        	}
        	return true;
        }
        //quxian:0，生产人员
        if(quxian.equals("0")){
        	String [] k={"/Sp/userlistall","/Sp/seleusersearch","/Sp/seleuserbyid","/Sp/adduser"};
        	List<String> List = Arrays.asList(k);
        	if(url.indexOf("dele")!=-1){//任何删除都会被拦截     
        		return false;
        	}
        	if(List.contains(url)){      
        		return false;
        	}
        	return true;
        }
		response.sendError(403, "您没有权限");
        return false;
    }
    
    //preHandle返回true后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

//    	Jy jy =new Jy();
//        jy.wary("http://localhost:7272/SchoolLogin.html");
    }

    //preHandle返回true后执行
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}
	
	public boolean shenchan(String url) {
        	String [] k={"/Sp/userlistall","/Sp/seleusersearch","/Sp/seleuserbyid","/Sp/adduser"};
        	List<String> List = Arrays.asList(k);
        	if(url.indexOf("dele")!=-1){     
        		return false;
        	}
        	if(List.contains(url)){      
        		return false;
        	}
        	return true;
	}
}
